﻿using System;

namespace ContextualStateless
{
    public partial class StateMachine< TState, TTrigger, TContext >
    {
        internal class DynamicTriggerRepresentation : AbstractTriggerRepresentation
        {
            /// <summary>
            /// Next (destination) state calculating method.
            /// </summary>
            protected readonly Func<object[], TContext, TState> m_stateCalculatingMethod;

            #region Constructors

            public DynamicTriggerRepresentation( TTrigger trigger, Func< object[], TContext, TState > stateCalculatingMethod, Func< TContext, bool > guard, LogicalActionHandler contextHandler )
                : base( trigger, guard, contextHandler )
            {
                m_stateCalculatingMethod = Enforce.ArgumentNotNull(stateCalculatingMethod, "destination");
//                Contract.Requires( stateCalculatingMethod != null, "stateCalculatingMethod" );
//
//                m_stateCalculatingMethod = stateCalculatingMethod;
            }

            public DynamicTriggerRepresentation( TTrigger trigger, Func< object[], TContext, TState > stateCalculatingMethod, Func< TContext, bool > guard )
                : this( trigger, stateCalculatingMethod, guard, null )
            {
            }

            #endregion

            // -------------------------------------------------------------------------------------------------------------

            public override bool ResultsInTransitionFrom(TState source, object[] args, TContext context, out TState destination)
            {
                destination = m_stateCalculatingMethod( args, context );

                return true;
            }
        }
    }
}
